package team.nmsg.ge.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;


import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.support.RequestContext;


import team.nmsg.auto.TestLog;
import team.nmsg.ge.system.bean.PageObject;
import team.nmsg.ge.system.dao.ITestLogDao;
import team.nmsg.ge.system.dao.ITestLogRepository;
import team.nmsg.ge.system.service.ITestLogService;

@Service
@Transactional
public class TestLogServiceImpl implements ITestLogService{
	
	@Autowired
	ITestLogRepository rep;
	
	@Autowired
	ITestLogDao testLogDao;

	@Override
	public TestLog saveLog(TestLog toSave) {
		return testLogDao.saveLog(toSave);
	}

	@Override
	public PageObject<TestLog> queryLogs(int pageSize , int startIndex , TestLog businessLog,long startTime,long endTime) {
		PageObject<TestLog> page = new PageObject<TestLog>();
		long logCount = testLogDao.querylogCount(businessLog,startTime,endTime);
		page.setSumCount( new Long(logCount).intValue() );
		
		List<TestLog> queryUserList = testLogDao.QueryTestLogList(pageSize, startIndex, businessLog,startTime,endTime);
		
		page.setCurrentEntitys(queryUserList);
		
		return page;
	}
	
	@Override
	public boolean deleteLogById(String[] logIds) {
		
		try {
			for(int i=0;i<logIds.length;i++)
				testLogDao.deleteLogById(logIds[i]);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	@Override
	public List<TestLog> queryBaseLogsBySn(String sn) {
		return testLogDao.queryBaseLogsBySn(sn);
	}
	
	@Override
	public HSSFWorkbook getFileToBackUp(TestLog log, long _startTime, long _endTime, int pageNo , int pageSize,HttpServletRequest req) {
		List<TestLog> logs = new ArrayList<TestLog>();
		
		int startIndex = pageNo;
		if( startIndex!= -1 ){
			startIndex = pageSize*(  pageNo - 1);
		}
		
		List<TestLog> queryList = testLogDao.QueryTestLogList(pageSize, startIndex, log, _startTime, _endTime);
		logs.addAll(queryList);
		
		//创建 MO
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("log");
		sheet.setColumnWidth(0, 30 * 160);
		sheet.setColumnWidth(1, 60 * 160);
		sheet.setColumnWidth(2, 60 * 160);
		sheet.setColumnWidth(3, 60 * 160);
		sheet.setColumnWidth(4, 60 * 160);
		sheet.setColumnWidth(5, 60 * 160);
		sheet.setColumnWidth(6, 60 * 160);
		sheet.setColumnWidth(7, 60 * 160);
		sheet.setColumnWidth(8, 60 * 160);
		
		HSSFCellStyle styleTitle = wb.createCellStyle();
		styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 16);
		styleTitle.setFont(font);
		
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		RequestContext requestContext = new RequestContext(req);
		
		HSSFRow row = sheet.createRow((int) 0);
		row.setHeight( (short) (2 * 256));
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(styleTitle);
		cell = row.createCell(1);
		cell.setCellValue("设备SN");
		cell.setCellStyle(styleTitle);
		cell = row.createCell(2);
		cell.setCellValue("测试类型");
		cell.setCellStyle(styleTitle);
		cell = row.createCell(3);
		cell.setCellValue("操作时间");
		cell.setCellStyle(styleTitle);
		cell = row.createCell(4);
		cell.setCellValue("耗时(s)");
		cell.setCellStyle(styleTitle);
		cell = row.createCell(5);
		cell.setCellValue("操作描述");
		cell.setCellStyle(styleTitle);
		cell = row.createCell(6);
		cell.setCellValue("结果");
		cell.setCellStyle(styleTitle);
		cell = row.createCell(7);
		cell.setCellValue("测试类别");
		cell.setCellStyle(styleTitle);
		cell = row.createCell(8);
		cell.setCellValue("用户");
		cell.setCellStyle(styleTitle);
		
		
		if( !CollectionUtils.isEmpty(logs) ){
			int size = logs.size();
			for( int i=0; i<size ;i++ ){
				TestLog tmpLog = logs.get(i);
			
				row = sheet.createRow( (i+1) );
				row.setHeight( (short) (1.5 * 256));
				
				cell = row.createCell(0);
				cell.setCellValue((i+1) );
				cell.setCellStyle(style);
				cell = row.createCell(1);
				cell.setCellValue(tmpLog.getDevSn());
				cell.setCellStyle(style);
				cell = row.createCell(2);
				cell.setCellValue("");
				if(tmpLog.getType()==1){
					cell.setCellValue("基测");
				}	
				if(tmpLog.getType()==2){
					cell.setCellValue("复测");
				}
				cell.setCellStyle(style);
				cell = row.createCell(3);
				cell.setCellValue(tmpLog.formatDate(tmpLog.getTimeStamp()));
				cell.setCellStyle(style);
				
				cell = row.createCell(4);
				cell.setCellValue(tmpLog.getCostTime());
				cell.setCellStyle(style);
				
				cell = row.createCell(5);
				cell.setCellValue(tmpLog.getDescr());
				cell.setCellStyle(style);
				
				cell = row.createCell(6);
				cell.setCellValue(tmpLog.getRes());
				cell.setCellStyle(style);
				
				cell = row.createCell(7);
				cell.setCellValue(tmpLog.getTesttype());
				cell.setCellStyle(style);
				
				cell = row.createCell(8);
				cell.setCellValue(tmpLog.getUser());
				cell.setCellStyle(style);
				
			}
		}
		
		return wb;
	}
}
